package com.example.administrator.printoutindemo.mvp.presenter;

import android.app.Application;

import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.utils.RxLifecycleUtils;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;

import javax.inject.Inject;

import java.util.Map;

import com.example.administrator.printoutindemo.mvp.contract.Login_GongJuContract;


import com.example.administrator.printoutindemo.mvp.ui.entity.Login_GongJu;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;


@ActivityScope
public class Login_GongJuPresenter extends BasePresenter<Login_GongJuContract.Model, Login_GongJuContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public Login_GongJuPresenter(Login_GongJuContract.Model model, Login_GongJuContract.View rootView) {
        super(model, rootView);
    }

    public void getLogin_GongJuData(Map<String, String> map) {

        try {
            mModel.getLogin_GongJuData(map).subscribeOn(Schedulers.io())
                    //                .retryWhen(new RetryWithDelay(3, 2))//遇到错误时重试,第一个参数为重试几次,第二个参数为重试的间隔
                    .doOnSubscribe(disposable -> {
                        mRootView.showLoading();
                    }).subscribeOn(AndroidSchedulers.mainThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .doFinally(() -> {
                        mRootView.hideLoading();
                    })
                    .compose(RxLifecycleUtils.bindToLifecycle(mRootView))//使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                    .subscribe(new ErrorHandleSubscriber<Login_GongJu>(mErrorHandler) {
                        @Override
                        public void onNext(Login_GongJu entityData) {
                            mRootView.getLogin_GongJuDataSuccess(entityData);
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }
}
